<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
            <el-form-item label="发票代码" prop="invoiceCode">
                <el-input v-model="queryParams.invoiceCode" placeholder="请输入发票代码" clearable @keyup.enter.native="handleQuery"/>
            </el-form-item>
            <el-form-item label="服务名称" prop="name">
                <el-input v-model="queryParams.name" placeholder="请输入服务名称" clearable @keyup.enter.native="handleQuery"/>
            </el-form-item>
            <el-form-item label="企业名称" prop="companyName">
                <el-input v-model="queryParams.companyName" placeholder="请输入企业名称" clearable @keyup.enter.native="handleQuery"/>
            </el-form-item>
            <el-form-item label="税号" prop="taxNum">
                <el-input v-model="queryParams.taxNum" placeholder="请输入纳税识别号" clearable @keyup.enter.native="handleQuery"/>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <TableCard>
            <el-row :gutter="10" class="mb8">
                <el-col :span="1.5">
                    <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['oa:invoice:add']">
                        新增
                    </el-button>
                </el-col>
                <el-col :span="1.5">
                    <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate" v-hasPermi="['oa:invoice:edit']">
                        修改
                    </el-button>
                </el-col>
                <el-col :span="1.5">
                    <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="['oa:invoice:remove']">
                        删除
                    </el-button>
                </el-col>
                <el-col :span="1.5">
                    <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['oa:invoice:export']">
                        导出
                    </el-button>
                </el-col>
                <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
            </el-row>

            <el-table :border="Global.tableShowBorder" v-loading="loading" :data="invoiceList" @selection-change="handleSelectionChange">
                <el-table-column type="selection" width="55" align="center"/>
                <el-table-column label="企业名称" align="center" prop="companyName" :show-overflow-tooltip="true"/>
                <el-table-column label="纳税识别号" align="center" prop="taxNum" :show-overflow-tooltip="true"/>
                <el-table-column label="发票金额" align="center" prop="amt"/>
                <el-table-column label="发票代码" align="center" prop="invoiceCode"/>
                <el-table-column label="发票号码" align="center" prop="invoiceNum"/>
                <el-table-column label="税率" align="center" prop="taxRate"/>
                <el-table-column label="服务名称" align="center" prop="name" :show-overflow-tooltip="true"/>
                <el-table-column label="地址" align="center" prop="address" :show-overflow-tooltip="true"/>
                <el-table-column label="电话" align="center" prop="phone"/>
                <el-table-column label="银行" align="center" prop="bank"/>
<!--                <el-table-column label="收款人" align="center" prop="payee"/>-->
<!--                <el-table-column label="复核人" align="center" prop="examinePerson"/>-->
<!--                <el-table-column label="开票人" align="center" prop="drawer"/>-->
<!--                <el-table-column label="备注" align="center" prop="remark"/>-->
                <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                    <template slot-scope="scope">
                        <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['oa:invoice:edit']">
                            修改
                        </el-button>
                        <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['oa:invoice:remove']">
                            删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>

            <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList"/>
        </TableCard>

        <!-- 添加或修改发票管理对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="1200px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="120px">
                <el-form-item label="发票文件地址">
                    <image-upload v-model="form.url" :limit="1"/>
                </el-form-item>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="企业名称" prop="companyName">
                            <el-input v-model="form.companyName" placeholder="请输入企业名称"/>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="纳税识别号" prop="taxNum">
                            <el-input v-model="form.taxNum" placeholder="请输入纳税识别号"/>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="电话" prop="phone">
                            <el-input v-model="form.phone" placeholder="请输入电话"/>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="银行" prop="bank">
                            <el-input v-model="form.bank" placeholder="请输入银行"/>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="24">
                        <el-form-item label="地址" prop="address">
                            <el-input v-model="form.address" type="textarea" :rows="5" placeholder="请输入内容"/>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="发票金额" prop="amt">
                            <el-input v-model="form.amt" placeholder="请输入发票金额"/>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="开票时间" prop="invoiceDate">
                            <el-date-picker clearable v-model="form.invoiceDate" type="date" value-format="yyyy-MM-dd" placeholder="请选择开票时间">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="发票代码" prop="invoiceCode">
                            <el-input v-model="form.invoiceCode" placeholder="请输入发票代码"/>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="发票号码" prop="invoiceNum">
                            <el-input v-model="form.invoiceNum" placeholder="请输入发票号码"/>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="税率" prop="taxRate">
                            <el-input v-model="form.taxRate" placeholder="请输入税率"/>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="税额" prop="taxAmt">
                            <el-input v-model="form.taxAmt" placeholder="请输入税额"/>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="服务名称" prop="name">
                            <el-input v-model="form.name" placeholder="请输入服务名称"/>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="收款人" prop="payee">
                            <el-input v-model="form.payee" placeholder="请输入收款人"/>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="复核人" prop="examinePerson">
                            <el-input v-model="form.examinePerson" placeholder="请输入复核人"/>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="开票人" prop="drawer">
                            <el-input v-model="form.drawer" placeholder="请输入开票人"/>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="24">
                        <el-form-item label="备注" prop="remark">
                            <el-input v-model="form.remark" type="textarea" :rows="5" placeholder="请输入内容"/>
                        </el-form-item>
                    </el-col>
                </el-row>

            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import {listInvoice, getInvoice, delInvoice, addInvoice, updateInvoice} from "@/api/oa/invoice";

export default {
    name: "Invoice",
    data() {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 发票管理表格数据
            invoiceList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 20,
                orderByColumn: "createTime",
                isAsc: "descending",
                url: null,
                amt: null,
                invoiceDate: null,
                invoiceCode: null,
                invoiceNum: null,
                taxRate: null,
                taxAmt: null,
                name: null,
                companyName: null,
                taxNum: null,
                address: null,
                phone: null,
                bank: null,
                payee: null,
                examinePerson: null,
                drawer: null,
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {
                url: [
                    {required: true, message: "发票文件地址不能为空", trigger: "blur"}
                ],
            }
        };
    },
    created() {
        this.getList();
    },
    methods: {
        /** 查询发票管理列表 */
        getList() {
            this.loading = true;
            listInvoice(this.queryParams).then(response => {
                this.invoiceList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                id: null,
                url: null,
                amt: null,
                invoiceDate: null,
                invoiceCode: null,
                invoiceNum: null,
                taxRate: null,
                taxAmt: null,
                name: null,
                companyName: null,
                taxNum: null,
                address: null,
                phone: null,
                bank: null,
                payee: null,
                examinePerson: null,
                drawer: null,
                createBy: null,
                createTime: null,
                updateBy: null,
                updateTime: null,
                remark: null
            };
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.id)
            this.single = selection.length !== 1
            this.multiple = !selection.length
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            this.open = true;
            this.title = "添加发票管理";
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset();
            const id = row.id || this.ids
            getInvoice(id).then(response => {
                this.form = response.data;
                this.open = true;
                this.title = "修改发票管理";
            });
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs["form"].validate(valid => {
                if (valid) {
                    if (this.form.id != null) {
                        updateInvoice(this.form).then(response => {
                            this.$modal.msgSuccess("修改成功");
                            this.open = false;
                            this.getList();
                        });
                    } else {
                        addInvoice(this.form).then(response => {
                            this.$modal.msgSuccess("新增成功");
                            this.open = false;
                            this.getList();
                        });
                    }
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.id || this.ids;
            this.$modal.confirm('是否确认删除发票管理编号为"' + ids + '"的数据项？').then(function () {
                return delInvoice(ids);
            }).then(() => {
                this.getList();
                this.$modal.msgSuccess("删除成功");
            }).catch(() => {
            });
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download('oa/invoice/export', {
                ...this.queryParams
            }, `invoice_${new Date().getTime()}.xlsx`)
        }
    }
};
</script>
